package com.zb.goods.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.FieldSelectVo;
import com.zb.result.Result;
import com.zb.service.ZjGoodsParamFieldService;
import com.zb.service.ZjGoodsParamService;
import com.zb.utils.TokenUtil;
import com.zb.vo.GoodsParamFieldVo;
import com.zb.vo.GoodsParamSelectVo;
import com.zb.vo.GoodsParamVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品参数表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Api(tags = "商品参数类接口")
@RestController
@RequestMapping("/goods/set/param")
public class ZjGoodsParamController {
    @Resource
    private ZjGoodsParamService goodsParamService;
    @Resource
    private ZjGoodsParamFieldService goodsParamFieldService;

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "商品设置-商品参数 - 新增或修改商品参数")
    @ApiOperation("新增或修改商品参数")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid GoodsParamVo paramVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        goodsParamService.save(custAccId, paramVo);
        return Result.success(null == paramVo.getId()? "新增商品参数成功" : "修改商品参数成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品设置-商品参数 - 获取商品参数列表")
    @ApiOperation("获取商品参数列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody GoodsParamSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取商品参数列表成功").data(goodsParamService.getList(custAccId, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "商品设置-商品参数 - 批量禁用或启用商品参数")
    @ApiOperation("批量禁用或启用商品参数")
    @PutMapping("/")
    public Result disable(@RequestParam @ApiParam(value = "参数状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "商品参数ID集合", required = true) List<Long> idList) {
        goodsParamService.updateBatchDisableByIds(idList, status);
        return Result.success(status? "禁用商品参数成功" : "启用商品参数成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.DELETE, remark = "商品设置-商品参数 - 批量删除商品参数")
    @ApiOperation("批量删除商品参数")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "商品参数ID集合", required = true) List<Long> idList) {
        goodsParamService.deleteByIds(idList);
        return Result.success("删除成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "商品设置-商品参数字段 - 新增或修改商品参数字段")
    @ApiOperation("新增或修改商品参数字段")
    @PostMapping("/saveParamField")
    public Result saveParamField(HttpServletRequest request, @RequestBody @Valid GoodsParamFieldVo paramFieldVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        goodsParamFieldService.save(custAccId, paramFieldVo);
        return Result.success(null == paramFieldVo.getId()? "新增商品字段成功" : "修改商品字段成功");
    }

    @ApiOperation("获取参数字段列表")
    @PostMapping("/{paramId}")
    public Result getParamFieldList(@PathVariable @ApiParam(value = "商品参数ID", required = true) Long paramId,
                                    @RequestBody @Valid FieldSelectVo selectVo) {
        return Result.success("获取参数字段列表成功").data(goodsParamFieldService.getParamFieldList(paramId, selectVo));
    }

    @ApiOperation("获取参数字段信息")
    @GetMapping("/getParamFieldInfo/{id}")
    public Result getParamFieldInfo(@PathVariable @ApiParam(value = "参数字段ID", required = true) Long id) {
        return Result.success("获取参数字段信息成功！").data("row", goodsParamFieldService.getParamFieldInfo(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "商品设置-商品参数字段 - 批量禁用或启用参数字段")
    @ApiOperation("批量禁用或启用参数字段")
    @PutMapping("/disableParamField")
    public Result disableParamField(@RequestParam @ApiParam(value = "参数字段状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                                    @RequestBody @ApiParam(value = "参数字段ID集合", required = true) List<Long> idList) {
        goodsParamFieldService.updateBatchDisableByIds(idList, status);
        return Result.success(status? "禁用参数字段成功" : "启用参数字段成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "商品设置-商品参数字段 - 批量删除参数字段")
    @ApiOperation("批量删除参数字段")
    @DeleteMapping("/deleteParamField")
    public Result deleteParamField(@RequestBody @ApiParam(value = "参数字段ID集合", required = true) List<Long> idList) {
        goodsParamFieldService.deleteByIds(idList);
        return Result.success("删除参数字段成功");
    }

    @ApiOperation("根据参数ID获取参数字段")
    @GetMapping("/getParamFields/{paramId}")
    public Result getParamFields(@PathVariable @ApiParam(value = "参数ID", required = true) Long paramId) {
        return Result.success("获取参数字段数据成功").data("rows", goodsParamFieldService.pageSelect(paramId));
    }



}
